﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using MettleSystems.dashCommerce.Store;

namespace MettleSystems.dashCommerce.Core
{
	public abstract class WebApplicationBase : System.Web.HttpApplication
	{
		#region Static Properties

		protected const string SiteSettingsKey = "SiteSettings";
		protected const string DEFAULT_ProductName = "OpenNetCommerce";
		protected static string _ProductName = DEFAULT_ProductName;

		/// <summary>
		/// Gets the Product name from the assembly AssemblyProductAttribute.
		/// </summary>
		public static string ProductName
		{
			get
			{
				return _ProductName;
			}
		}

		private static SiteSettings _siteSettings = new SiteSettings();

		/// <summary>
		/// Gets the current <see cref="MettleSystems.dashCommerce.Store.SiteSettings"/>.
		/// </summary>
		public static SiteSettings SiteSettings
		{
			get { return _siteSettings; }
			protected set
			{
				lock (_siteSettings) {
					_siteSettings = value;
				}
			}
		}

		#endregion

		#region Methods

		/// <summary>
		/// Handles the Start event of the Application control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
		protected virtual void Application_Start(object sender, EventArgs e)
		{
			Assembly currentAssembly = System.Reflection.Assembly.GetExecutingAssembly();
			AssemblyProductAttribute apa = AssemblyProductAttribute.GetCustomAttribute(currentAssembly, typeof(AssemblyProductAttribute)) as AssemblyProductAttribute;
			_ProductName = apa == null ? DEFAULT_ProductName : apa.Product;

			try {
				SiteSettings = SiteSettings.Load();
			}
			catch {
				//we shouldn't need this because it is loaded automatically
				//SiteSettings = new SiteSettings();
			}
		}

		#endregion
	}
}
